package com.yocard.yxyd.ui.presenter;

import android.support.v4.app.FragmentActivity;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.http.model.response.BannerBean;
import com.yocard.yxyd.utils.RxUtil;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.ui.contract.OrderContract;
import com.zaaach.citypicker.CityPicker;
import com.zaaach.citypicker.adapter.OnPickListener;

import javax.inject.Inject;

/**
 * Created on 2019/4/2.
 */
public class OrderPresenter extends RxPresenter<OrderContract.View> implements OrderContract.Presenter {

    @Inject
    public OrderPresenter(HttpManager manager) {
        super(manager);
    }


    @Override
    public void switchCity(OnPickListener listener, FragmentActivity activity) {
        CityPicker.from(activity)
                .enableAnimation(true)
                .setAnimationStyle(R.style.DefaultCityPickerAnimation)
                .setLocatedCity(null)
                .setOnPickListener(listener)
                .show();
    }

    @Override
    public void initLocation() {
        AndPermission.with(AppContext.getContext())
                .runtime()
                .permission(Permission.ACCESS_FINE_LOCATION)
                .onGranted(permissions -> {
                    AMapLocationClient locationClient = new AMapLocationClient(AppContext.getContext());
                    AMapLocationClientOption mOption = new AMapLocationClientOption();
                    mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
                    mOption.setGpsFirst(false);
                    mOption.setNeedAddress(true);
                    mOption.setHttpTimeOut(10000);
                    mOption.setInterval(2000);
                    mOption.setOnceLocation(true);
                    locationClient.setLocationOption(mOption);
                    mView.onLocationInit(locationClient);
                })
                .onDenied(permissions -> {
                    ToastUtils.showLong("请授予APP定位权限");
                })
                .start();
    }

    @Override
    public void getBanners() {
        addSubscribe(httpManager.getBanners().compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<BannerBean>(mView) {
                    @Override
                    public void onSuccess(BannerBean bannerBean) {
                        try {
                            mView.getBanners(bannerBean);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }));
    }
}
